var lng = $.util.getRequest("lng");
var lat = $.util.getRequest("lat");
// 百度地图API功能
var map = new BMap.Map("allmap")

map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
//map.enableContisdfnuousZoom();    //启用地图惯性拖拽，默认禁用
map.setDefaultCursor("pointer");
if (lng.length != 0) {
    // 网点编辑时，地图中心设置为该网点
    map.addOverlay(new BMap.Marker(new BMap.Point(lng, lat)));
    map.centerAndZoom(new BMap.Point(lng, lat), 15);
} else {
    // 新增网点时设置网点的默认坐标为全河北
    map.centerAndZoom("河北", 12);
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            map.panTo(r.point);
        }
        else {
            alert("自动定位失败，请输入关键词进行手动检索");
        }
    }, {enableHighAccuracy: true})
}
//单击获取点击的经纬度
map.addEventListener("click", function (e) {
    map.clearOverlays();
    point = new BMap.Point(e.point.lng, e.point.lat);
    map.addOverlay(new BMap.Marker(point));

    //var pt = e.point;
    new BMap.Geocoder().getLocation(point, function(rs){
        var addComp = rs.addressComponents;
        var address = addComp.province+addComp.city+addComp.district+addComp.street+addComp.streetNumber;
        $("#orgAddress").val(address);
    });
    $("#longitude").val(e.point.lng);
    $("#latitude").val(e.point.lat);
});
var local = new BMap.LocalSearch(map, {
    renderOptions: {map: map}
});
var searchText = document.getElementById("pointSearch");
var pointButton = document.getElementById("doPointSearch");
pointButton.onclick = function (ev) {
    local.search(searchText.value);
}
searchText.onkeypress = function (ev) {
    if (ev.keyCode) {
        local.search(searchText.value);
    }
}